package org.br.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.entity.Scmpaymentlinkvendor;
import org.br.erp.base.mapper.ScmpaymentlinkvendorMapper;
import org.br.erp.base.service.IScmpaymentlinkvendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 付款条款关联供应商
 * @Author: jeecg-boot
 * @Date:   2023-01-05
 * @Version: V1.0
 */
@Service
public class ScmpaymentlinkvendorServiceImpl extends ServiceImpl<ScmpaymentlinkvendorMapper, Scmpaymentlinkvendor> implements IScmpaymentlinkvendorService {

    @Autowired
    ScmpaymentlinkvendorMapper scmpaymentlinkvendorMapper;
    @Override
    @Transactional
    public void saveLinkVendorid(String companyid, String paymentid, String vendorid) {
        if(vendorid==null){
            throw  new RuntimeException("请查看供应商是否选择正确！");
        }
        String[] split = vendorid.split(",");
        //先删除现有条款
        QueryWrapper<Scmpaymentlinkvendor> scmpaymentlinkvendorQueryWrapper=new QueryWrapper<>();
        scmpaymentlinkvendorQueryWrapper.eq("companyid",companyid);
        scmpaymentlinkvendorQueryWrapper.eq("paymentid",paymentid);
        scmpaymentlinkvendorMapper.delete(scmpaymentlinkvendorQueryWrapper);


        for (String s : split) {
            Scmpaymentlinkvendor scmpaymentlinkvendor=new Scmpaymentlinkvendor();
            scmpaymentlinkvendor.setCompanyid(companyid);
            scmpaymentlinkvendor.setPaymentid(paymentid);
            scmpaymentlinkvendor.setVendorid(s);
            scmpaymentlinkvendorMapper.insert(scmpaymentlinkvendor);
        }
    }

    @Override
    @Transactional
    public void deletePaymentId(String paymentid) {
        QueryWrapper<Scmpaymentlinkvendor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("paymentid",paymentid);
        scmpaymentlinkvendorMapper.delete(queryWrapper);
    }

    @Override
    public String getLinkVendorid(String companyid, String paymentid) {
        QueryWrapper<Scmpaymentlinkvendor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("companyid",companyid);
        queryWrapper.eq("paymentid",paymentid);
        String res="";
        List<Scmpaymentlinkvendor> scmpaymentlinkvendors = scmpaymentlinkvendorMapper.selectList(queryWrapper);
        if(scmpaymentlinkvendors!=null && scmpaymentlinkvendors.size()>0){
            if(scmpaymentlinkvendors.size()>5){
                for(int i=0;i<5;i++){
                    res+=scmpaymentlinkvendors.get(i).getVendorid()+",";
                }
            }else{
                for (Scmpaymentlinkvendor scmpaymentlinkvendor : scmpaymentlinkvendors) {
                    res+=scmpaymentlinkvendor.getVendorid()+",";
                }
            }
        }
        if(res!=""){
            return res.substring(0,res.length()-1);
        }else {
            return res;
        }
    }

    @Override
    public String getPaymentid(String companyid, String vendorid) {
        QueryWrapper<Scmpaymentlinkvendor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("companyid",companyid);
        queryWrapper.eq("vendorid",vendorid);
        List<Scmpaymentlinkvendor> scmpaymentlinkvendors = scmpaymentlinkvendorMapper.selectList(queryWrapper);
        if(scmpaymentlinkvendors!=null && scmpaymentlinkvendors.size()>0){
            return scmpaymentlinkvendors.get(0).getPaymentid();
        }else
            return "";
    }
}
